package com.netease.xone.itemview;

import java.util.List;

import protocol.meta.AppInfo;
import protocol.meta.AppInfoDetail;
import protocol.meta.AppSpecial;
import protocol.meta.AppSpecialItem;
import Utils.StackTraceUtil;
import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.netease.log.NTLog;
import com.netease.xone.R;
import com.netease.xone.activity.ActivityInfoDetail;
import com.netease.xone.itemview.AbsLinearLayout.OnSelectListener;
import com.netease.xone.widget.LoadingImageView;


public class RenderInfoSpecial implements IRenderInfoBase, OnSelectListener{

    public static final String TAG = RenderInfoSpecial.class.getSimpleName();

    private int mWidth;
    private int mHeight;
    
    private View mRoot;
    private LoadingImageView mImage;
    private TextView  mTitle;
    private TextView  mText;
    private LinearLayout mContainer;
  
    public RenderInfoSpecial(View v) {
        mRoot = v;
        
        mWidth = v.getContext().getResources().getDisplayMetrics().widthPixels;
        mHeight = v.getContext().getResources().getDimensionPixelSize(R.dimen.info_size_160);
        
        mImage = (LoadingImageView)v.findViewById(R.id.image);
        mTitle = (TextView)v.findViewById(R.id.title);
        mText = (TextView)v.findViewById(R.id.text);
        mContainer = (LinearLayout)v.findViewById(R.id.container);
    }

    @Override
    public void renderView(Context context, AppInfo appInfo) {
        renderView(context, appInfo.getSpecial(), appInfo.getInfoId(), false, false);
    }

    @Override
    public void renderView(Context context, AppInfoDetail appInfoDetail) {
    }
    
    /**
     * 
     * @param context
     * @param special
     * @param infoId
     * @param detail 表示是否正文模式
     * @param showAll
     */
    public void renderView(Context context, AppSpecial special, String infoId, boolean detail, boolean showAll) {
        NTLog.d(TAG, StackTraceUtil.getMethodName());
        
        mImage.setVisibility(View.GONE);
        mTitle.setVisibility(View.GONE);
        mText.setVisibility(View.GONE);
        
        if (null == special) {
            mRoot.setVisibility(View.GONE);
            return;
        }
        
        if (!TextUtils.isEmpty(special.getImageUrl()) && detail) {
            mImage.setVisibility(View.VISIBLE);
            mImage.setServerClipSize(mWidth, mHeight);
            mImage.setLoadingImage(special.getImageUrl());
        }
        
        String value = null;
        value = special.title;
        if (!TextUtils.isEmpty(value) && detail) {
            mTitle.setVisibility(View.VISIBLE);
            mTitle.setText(value);
        }
        
        value = special.description;
        if (!TextUtils.isEmpty(value) && detail) {
            mText.setVisibility(View.VISIBLE);
            mText.setText(value);
        }
            
        List<AppSpecialItem> list = special.getItemList();
        mContainer.removeAllViews();
        if (null != list && 0 < list.size()) {
            mRoot.setVisibility(View.VISIBLE);
            SpecialView specialView = null;
            specialView = new SpecialView(context, special, infoId, showAll);
            specialView.setOnSelectListener(this);
            
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            mContainer.addView(specialView, params);
        } else {
            mRoot.setVisibility(View.GONE);
            return;
        }
    }
    
    private void startDetailInfo(View v, int index) {
        NTLog.d(TAG, StackTraceUtil.getMethodName());
        // TODO 跳转infoId需要和IOS确认是否正确
        View view = v.findViewById(R.id.title);
        if (view.getTag() instanceof AppSpecialItem) {
            AppSpecialItem item = (AppSpecialItem)view.getTag();
            ActivityInfoDetail.startActivity(v.getContext(), item.getRefInfoId(), ActivityInfoDetail.MODE_FROM_INFO);
        } else if (view.getTag() instanceof String) {
            String infoId = (String)view.getTag();
            ActivityInfoDetail.startActivity(v.getContext(), infoId, ActivityInfoDetail.MODE_FROM_INFO);
        }
    }
    @Override
    public void onSelected(View v, int index) {
        startDetailInfo(v, index);
    }

    @Override
    public void onReSelected(View v, int index) {
        startDetailInfo(v, index);
    }
    
    public static class SpecialView extends AbsLinearLayout {
        
        private boolean showAll;
        private int defaultItemNum = 4;
        private int dividerHeight;
        private String infoId;
        private AppSpecial special;

        public SpecialView(Context context, AppSpecial special, String infoId, boolean showAll) {
            super(context);
            this.showAll = showAll;
            this.dividerHeight = context.getResources().getDimensionPixelSize(R.dimen.info_size_2);
            this.special = special;
            this.infoId = infoId;
            setOrientation(VERTICAL);
            show();
        }

        @Override
        public int getDividerStyle() {
            return AbsLinearLayout.DIVIDER_HEAD|AbsLinearLayout.DIVIDER_MIDDLE|AbsLinearLayout.DIVIDER_FOOT;
        }

        @Override
        public View getDivider() {
            View v = null;
            v = new ImageView(getContext());
            v.setBackgroundColor(getContext().getResources().getColor(R.color.button_gray));
            v.setPadding(0, dividerHeight, 0, 0);
            return v;
        }

        @Override
        public int getCount() {
            int count = 0;
            
            if (showAll) {
                count = special.getItemList().size();
            } else {
                count = special.getItemList().size() > defaultItemNum ? defaultItemNum : special.getItemList().size();
            }
            
            return count;
        }

        @Override
        public View getView(int position) {
            View child= null;
            TextView textView = null;
            String value = null;
            AppSpecialItem item = special.getItemList().get(position);
            
            if (position == defaultItemNum-1 && ( special.getItemList().size() >= defaultItemNum) && !showAll) {
                child = View.inflate(getContext(), R.layout.item_view_info_special_item1, null);
                child.findViewById(R.id.title).setTag(infoId);
            } else {
                child = View.inflate(getContext(), R.layout.item_view_info_special_item, null);
                textView = (TextView)child.findViewById(R.id.title);
                textView.setText(item.getTitle());
                textView.setTag(item);
                
                LoadingImageView image = (LoadingImageView)child.findViewById(R.id.image);
                value = item.getImageUrl();
                image.setLoadingImage(value);
            }
            
            return child;
        }
        
    }
}
